package com.itcm.purchase.common.utils;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itcm.purchase.entity.dto.PageDto;

import java.util.Map;

public class PageUtil {
    public static <T> Page<T> getPageParam(PageDto page, Class<T> tclass) {
        Integer pageNum = page.getPageNumber();
        Integer pageSize = page.getPageSize();
        if (pageNum == null || pageNum == 0) {
            pageNum = 1;
        }
        if (pageSize == null || pageSize == 0) {
            pageSize = 20;
        }
        Page<T> pageParam = new Page(pageNum, pageSize);
        if (page.getOrderByMap() != null) {
            for (Map.Entry<String, Boolean> entry : page.getOrderByMap().entrySet()) {
                String orderByK = entry.getKey();
                Boolean orderByV = entry.getValue();
                if (orderByV) {
                    pageParam = pageParam.setAsc(MyStringUtil.HumpToUnderline(orderByK));
                } else {
                    pageParam = pageParam.setDesc(MyStringUtil.HumpToUnderline(orderByK));
                }
            }
        }
        return pageParam;
    }
}
